package com.EWS.controller;

import com.EWS.model.UserDto;
import com.EWS.service.UserService;
import com.EWS.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

// 与前端交互在单词库页面进行各种操作
@WebServlet("/WordListServlet")
public class WordListServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    // 转换单词库页面的信息，添加单词，删除单词
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 通过前端登录时提交的会话信息获取此时登录的用户名
        HttpSession session = req.getSession(false); // 不创建新会话，过去前端会话信息
        String userName = (String) session.getAttribute("userName");
        String operate = req.getParameter("operate");

        // 获取前端输入的单词信息
        String word = req.getParameter("word");
        String definition = req.getParameter("definition");

        // 点击查询，不是就运行下面的添加单词
        if(operate.equals("search")) {
            // 刷新单词库界面
            List<UserDto> userDtoList = userService.getUserStudy(userName,word);
            req.setAttribute("userDtoList",userDtoList);
            req.getRequestDispatcher("wordsList.jsp").forward(req,resp);
            System.out.println("查询单词成功");
            return;
        }

        // 点击添加单词
        int result = userService.addWord(userName,word,definition);
        if (result>0){
            // 刷新单词库界面
            List<UserDto> userDtoList = userService.getUserStudy(userName,"");
            req.setAttribute("userDtoList",userDtoList);
            req.getRequestDispatcher("wordsList.jsp").forward(req,resp);
            System.out.println("添加单词成功");
        }
        else {
            System.out.println("添加单词失败");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 通过前端登录时提交的会话信息获取此时登录的用户名
        HttpSession session = req.getSession(false); // 不创建新会话，过去前端会话信息
        String userName = (String) session.getAttribute("userName");

        // 获取前端操作指令
        String opsCode = req.getParameter("opsCode");
        String word = req.getParameter("word");
        // 点击删除单词
        if("delWord".equals(opsCode)){
            int result = userService.deleteWord(userName,word);
            if(result>0){
                // 刷新单词库界面
                List<UserDto> userDtoList = userService.getUserStudy(userName,"");
                req.setAttribute("userDtoList",userDtoList);
                req.getRequestDispatcher("wordsList.jsp").forward(req,resp);
                System.out.println("删除单词成功");
            }
            else{
                System.out.println("删除单词失败");
            }
        }
    }
}
